package com.polaris.live.ui.live.widget

import android.animation.ObjectAnimator
import android.animation.PropertyValuesHolder
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.animation.LinearInterpolator
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.lifecycle.viewModelScope
import com.polaris.live.common.constant.PagConst
import com.polaris.live.common.manager.startCountdown
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.request
import com.polaris.live.databinding.WidgetLuckyBagBinding
import com.polaris.live.net.chatService
import com.polaris.live.resp.back_resp.luckybag.LuckyBagNewResp
import org.libpag.PAGScaleMode

/**
 * 直播间、派对房右下角福袋
 *
 * @author Created by 半仙 on 2024/6/13/013 10:59
 */
class LuckyBagView(context: Context, attributeSet: AttributeSet? = null) :
    ConstraintLayout(context, attributeSet) {

    private val mBinding = WidgetLuckyBagBinding.inflate(LayoutInflater.from(context), this, true)

    init {
        mBinding.pagLuckyBag.apply {
            path = PagConst.LIVE_LUCKY_BAG
            setRepeatCount(-1)
            setScaleMode(PAGScaleMode.Zoom)
            play()
        }
    }

    fun registerBagNum(viewModel: BaseViewModel, livdId: Long? = null) {
        viewModel.viewModelScope.startCountdown(Long.MAX_VALUE, 5000L, {
            viewModel.request({
                if (livdId == null) {
                    chatService.getLuckyBagNewReceive()
                } else {
                    chatService.getLuckyBagNewNum()
                }
            }, {
                setLuckyBagNum(it)
            })
        })
    }

    private fun setLuckyBagNum(luckyBagNewResp: LuckyBagNewResp) {
        mBinding.apply {
            if (luckyBagNewResp.number >= 99) {
                tvLuckyNum.text = "99+"
            } else {
                tvLuckyNum.text = luckyBagNewResp.number.toString()
            }
            if (luckyBagNewResp.number == 0) {
                tvLuckyNum.visibility = View.GONE
            } else {
                tvLuckyNum.visibility = View.VISIBLE
            }
        }
    }

    fun clearNum() {
        mBinding.tvLuckyNum.text = ""
        mBinding.tvLuckyNum.visibility = View.GONE
    }

    fun scaleAnimation() {
        val scaleAnimator = ObjectAnimator.ofPropertyValuesHolder(
            mBinding.tvLuckyNum,
            PropertyValuesHolder.ofFloat("scaleX", 1.0f, 1.5f, 1.0f),
            PropertyValuesHolder.ofFloat("scaleY", 1.0f, 1.5f, 1.0f)
        )
        scaleAnimator.interpolator = LinearInterpolator()
        scaleAnimator.duration = 300
        scaleAnimator.start()
    }

    fun setPageWidthHeight(width: Int, hegith: Int) {
        mBinding.pagLuckyBag.post {
            val layout = mBinding.pagLuckyBag.layoutParams as ConstraintLayout.LayoutParams
            layout.width = width
            layout.height = hegith
            layout.topMargin = 0
            mBinding.pagLuckyBag.layoutParams = layout
        }
    }

}